package tech.summerly.weather.component.activities

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.MenuItem
import kotlinx.android.synthetic.main.activity_aqi_detail.*
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.launch
import tech.summerly.weather.R
import tech.summerly.weather.component.adapter.PollutantsAdapter
import tech.summerly.weather.data.WeatherRepository
import tech.summerly.weather.data.bean.RealTime
import tech.summerly.weather.model.Region
import tech.summerly.weather.util.Aqi

class AqiDetailActivity : AppCompatActivity() {

    companion object {

        const val ARG_REGION = "region"
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_aqi_detail)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        val region: Region = intent.getParcelableExtra(ARG_REGION)
        supportActionBar?.title = region.fullName

        launch(UI) {
            val realTime: RealTime = WeatherRepository.getRealtimeWeather(region)
            recyclerPollutants.adapter = PollutantsAdapter(realTime)
            val aqiValue = realTime.aqi
            textAqiValue.text = aqiValue.toString()
            val aqi = Aqi.from(aqiValue)
            textEffects.text = aqi.healthEffects()
            textSuggest.text = aqi.suggest()
            textAqiLevel.text = aqi.localString()
        }
    }


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home) {
            onBackPressed()
            return true
        }
        return super.onOptionsItemSelected(item)
    }

}